package com.et.machine.broadcast

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.text.TextUtils
import android.util.Log
import com.et.logger.ELog
import com.et.logger.TAGS
import com.et.machine.manager.api.OutGoodsManager
import com.et.machine.manager.api.OutGoodsParamAndroid
import com.google.gson.Gson
import com.google.gson.annotations.SerializedName
import java.util.*

class LaunchBroadcast : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        if (TextUtils.equals(intent.action, ACTION)) {
            ELog.d(TAGS.machine, "machine:received launch broadcast")
            callback?.let {
                it(intent)
            }
        }

    }

    companion object {

        const val ACTION = "com.et.action.launch" // Don't allow changed

        private val receiver: LaunchBroadcast = LaunchBroadcast()
        private val intentFilter = IntentFilter().apply {
            addAction(ACTION)
        }

        fun register(ctx: Context) {
            ELog.d(TAGS.machine, "machine:launch broadcast register")
            ctx.registerReceiver(receiver, intentFilter)
        }

        fun unRegister(ctx: Context) {
            ELog.d(TAGS.machine, "machine:launch broadcast unregister")
            ctx.unregisterReceiver(receiver)
        }

        private var callback: ((intent: Intent) -> Unit)? = null

        fun addCallBack(call: (intent: Intent) -> Unit) {
            callback = call
        }
    }
}